package cn.edu.scau.cmi.linhongxin.client;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.edu.scau.cmi.linhongxin.adapter.*;


public class AdapterClient extends Application{

	public static void main() {
		// TODO Auto-generated method stub
		launch();
	}
	public Label studentIdLabel = new Label("学号");
    public TextField studentIdTextField = new TextField();
    public Label studentNameLabel = new Label("姓名");
    public TextField studentNameTextField = new TextField();
    public Label infoText = new Label("");
	public Button addAndSort=new Button("aad student");
	public Label my=new Label("林泓欣 201727010512");
	@Override
    public void start(Stage primaryStage) throws Exception{
		VBox root = new VBox();
        root.setSpacing(10);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root,780,580);
        primaryStage.setScene(scene);
        primaryStage.setTitle("适配器模式");
        
        addAndSort.setOnAction(handler -> {
            Integer studentId;
            try {
                studentId = Integer.parseInt(studentIdTextField.getText());
            } catch (Exception e) {
                infoText.setText("输入的学号应为数字");
                return;
            }
            String studentName = studentNameTextField.getText();
            student.put(studentId, studentName);
            infoText.setText(callAdapter());
        });
        HBox myHbox = new HBox();
        myHbox.getChildren().add(my);
        myHbox.setAlignment(Pos.BASELINE_CENTER);
        myHbox.setSpacing(30);
        root.getChildren().add(myHbox);
        
        // 输入框
        studentIdTextField.setPrefSize(200,30);
        HBox studentIdHbox = new HBox();       
        studentIdHbox.getChildren().add(studentIdLabel);
        studentIdHbox.getChildren().add(studentIdTextField);
        studentIdHbox.setAlignment(Pos.BASELINE_CENTER);
        studentIdHbox.setSpacing(10);
        root.getChildren().add(studentIdHbox);

        studentNameTextField.setPrefSize(200,30);
        HBox studentNameHbox = new HBox();
        studentNameHbox.getChildren().add(studentNameLabel);
        studentNameHbox.getChildren().add(studentNameTextField);
        studentNameHbox.setAlignment(Pos.BASELINE_CENTER);
        studentNameHbox.setSpacing(10);
        root.getChildren().add(studentNameHbox);

        // 创建按钮
        root.getChildren().add(addAndSort);

        // 提示信息
        ScrollPane sigscrollPane = new ScrollPane(infoText);
        sigscrollPane.setPrefSize(550, 200);
        HBox sigTextTipHbox = new HBox(sigscrollPane);
        sigTextTipHbox.setAlignment(Pos.CENTER);
        root.getChildren().add(sigTextTipHbox);

        primaryStage.show();

    }
	 Map<Integer, String> student = new HashMap<>();

	    public String callAdapter () {
	        List<Integer> studentIdList = new ArrayList<>(student.keySet());
	        StringBuilder info = new StringBuilder();
	        Students adapter;
	        // 调用类适配器
	        adapter = new ClassAdapter();
	        studentIdList = adapter.sortInt(studentIdList);
	        info.append("ClassAdapter\n");
	        for (int i = 0; i < studentIdList.size(); i++) {
	            Integer studentId = studentIdList.get(i);
	            String studentName = student.get(studentId);
	            info = info.append(studentId).append("  ")
	                    .append(studentName)
	                    .append("\n");
	        }
	        info.append("\n\n");
	        // 调用对象适配器
	        adapter = new ObjectAdapter(new SortUtil());
	        studentIdList= adapter.sortInt(studentIdList);
	        info.append("ObjectAdapter\n");
	        for (int i = 0; i < studentIdList.size(); i++) {
	            Integer studentId = studentIdList.get(i);
	            String studentName = student.get(studentId);
	            info = info.append(studentId).append("  ")
	                    .append(studentName)
	                    .append("\n");
	        }
	        return info.toString();
	    }
}
